from flask import Blueprint, jsonify, request

from models import Data
from schemas import DataSchema

bp = Blueprint('data', __name__, url_prefix='/data')


@bp.post('/get_data')
def get_data():
    page = request.form.get('page', type=int)
    limit = request.form.get('limit', type=int)

    if limit > 100:
        return jsonify({"code": 500, "msg": ""})

    query = Data.query.order_by(Data.create_time).paginate(page=page, per_page=limit, error_out=False)
    data = query.items

    res = {
        "code": 200,
        "msg": "",
        "count": query.total,
        "data": DataSchema(many=True).dump(data)
    }

    return jsonify(res)
